package com.oa.pm.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.joneying.common.web.response.Resp;
import com.oa.core.PageResult;
import com.oa.pm.dto.TopicAddDTO;
import com.oa.pm.dto.TopicDetailsListDTO;
import com.oa.pm.entity.Option;
import com.oa.pm.entity.Topic;
import com.oa.pm.service.IOptionService;
import com.oa.pm.service.ITopicService;
import com.oa.pm.vo.TopicDetailsVO;
import com.oa.pm.vo.TopicListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.Date;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/topic")
@Api(description = "题目表模块")
public class TopicController extends BaseController {

    private final ITopicService topicService;

    @Autowired
    private IOptionService optionService;

    @Autowired
    public TopicController(ITopicService topicService) {
        this.topicService = topicService;
    }

    @PostMapping("/list/ItemBankId")
    @CommonController
    @Description("根据试题库ID查询题目")
    @ApiOperation(value = "根据id查询")
    public Resp<TopicListVO> findTopicListByItemBankId(@ApiParam("页号") @RequestParam("pageNum") Integer pageNum,
                                          @ApiParam("行数") @RequestParam("pageSize") Integer pageSize,
                                          @ApiParam("试题库ID") @RequestParam("itemBankId") String itemBankId) {
        return new Resp().success(topicService.findTopicListByItemBankId(pageNum, pageSize, itemBankId));
    }

    /**
     * 获取详细信息
     *
     * @return Resp
     */
    @PostMapping("/findTopicDetails")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "查询详情")
    public Resp<TopicDetailsVO> findTopicDetailsList(TopicDetailsListDTO topicDetailsDTO) {
        return new Resp().success(topicService.findTopicDetailsList(topicDetailsDTO));
    }


    /**
     * 新增
     *
     * @param topic Topic
     * @return Resp
     */
    @PostMapping("/add")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "题目表对象") @RequestBody(required = false) TopicAddDTO topic) {
        topic.getTopic().setCreateTime(new Date());
        if (topicService.insert(topic.getTopic())) {
            if(topic.getOptions() != null && topic.getOptions().size() > 0){
                topic.getOptions().forEach(d -> d.setTopicId(topic.getTopic().getId()));
                if (optionService.insertBatch(topic.getOptions())) {
                    return new Resp().success();
                }
            }
            return new Resp().success();
        }
        return new Resp().failure();
    }

    /**
     * 根据Id修改
     *
     * @param topic Topic
     * @return Resp
     */
    @PutMapping("/update")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "题目表对象") @RequestBody(required = false) TopicAddDTO topic) {
        topic.getTopic().setUpdateTime(new Date());
        if (topicService.updateById(topic.getTopic())) {
            if(topic.getOptions() != null && topic.getOptions().size() > 0) {
                optionService.delete(new EntityWrapper<Option>().eq("topic_id", topic.getTopic().getId()));
                topic.getOptions().forEach(d -> d.setTopicId(topic.getTopic().getId()));
                if (optionService.insertBatch(topic.getOptions())) {
                    return new Resp().success();
                }
            }
            return new Resp().success();
        }
        return new Resp().failure();
    }

//    /**
//     * 根据Id删除
//     *
//     * @param id id
//     * @return Resp
//     */
//    @DeleteMapping("/{id}")
//    @CommonController
//    @Description("根据id删除")
//    @ApiOperation(value = "根据id删除")
//    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
//        Topic topic = topicService.selectById(id);
//        if (topic != null && topic.getIsDeleted() == 2) {
//            topic.setIsDeleted(1);
//            if (topicService.updateById(topic)) {
//                return new Resp().success();
//            }
//            return new Resp().failure();
//        }
//        return new Resp().failure();
//    }

}
